package liangtWorkSpace.springAndMybatis.api;

import liangtWorkSpace.mybatisTest.entity.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author liangt
 * @ClassName: TestStudent
 * @title: TestStudent
 * @projectName liangtWorkSpace.old.api
 * @description: TODO
 * @date 2020/10/10 11:39
 */
public class TestStudent {

    /**
     * 测试Mybatis--Student访问
     * @MethodName main
     * @param args
     * @return
     * @throws
     * @author liangt
     * @date 2020/10/10 11:40
     */
    public static void main(String[] args) throws IOException {

        //根据配置文件获取数据库相关信息
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        List<Student> studentList = sqlSession.selectList("listStudent");

        for (Student student : studentList){
            System.out.println("ID = " + student.getId() + ",Name = " + student.getName() );
        }
    }
}
